import java.util.Arrays;

public class Test {

    public static int ayy(String s){//字符串中的第一个唯一字符
        char[] ch=s.toCharArray();
        int[] count=new int[126];
        int n=ch.length;
        for(int i=0;i<n;i++){
            char c=ch[i];
            count[c-'a']++;
        }
        for(int i=0;i<n;i++){
            char c=ch[i];
            if(count[c-'a']==1)return i;
        }
        return -1;
    }

    public static void main(String[] args) {
        String a="loveleetcode";
        System.out.println(ayy(a));//字符串中的第一个唯一字符
    }


    public static void main8(String[] args) {//StringBuilder的使用
        StringBuilder str=new StringBuilder();
        str.append("abc").append("def").append("ghi");
        str.append("hello");
        str.reverse();//逆转
        System.out.println(str);
    }
    public static void main6(String[] args) {
        //字符串截取
        String str="abcdef";
        String sub=str.substring(0,2);//左闭右开
        String sub2=str.substring(2);//从下标2开始截取
        System.out.println(sub);
        System.out.println(sub2);

        char[] ch=new char[]{'a','b','c','d','e','f'};
        String str2=new String(ch);
        str2=str2.intern();//手动入池
        System.out.println(str==str2);
    }
    public static void main5(String[] args) {
        //字符串拆分
        String str="hello world,how are you";
        String[] ret=str.split(" |,");//可以再加一个参数，表示最多分几组
        String str1="192.168.1.1";
        String[] ret2=str1.split("\\.");
        for(String s:ret){
            System.out.println(s);
        }
        for(String s:ret2){
            System.out.println(s);
        }
        System.out.println("===================");
        //利用循环进行多次分割
        String str2="name=zhangsan&age=18";
        String[] ret3=str2.split("&");
        for(String s:ret3){
            String[] arr=s.split("=");
            for(String a:arr){
                System.out.println(a);
            }
        }
    }



    public static void main4(String[] args) {
        //字符串替换
        String str="helloworld";
        System.out.println(str.replaceFirst("l","_"));
        System.out.println(str.replaceAll("l",""));
        System.out.println(str.replace("ll","_"));
    }




    public static void main3(String[] args) {
        String str = "Hello World";
        String ret=str.substring(0,str.length()-1);
        String ret1=str.toUpperCase();//转大写
        String ret2=str.toLowerCase();//转小写
        char[] arr=str.toCharArray();//字符串转数组
        System.out.println(Arrays.toString(arr));//遍历1
        for(char c:arr){//遍历2
            System.out.print(c+" ");
        }
        System.out.println();
        System.out.println(ret);
        System.out.println(ret1);
        System.out.println(ret2);

        System.out.println("=============");
        //数组转字符串
        char[] arr1={'a','b','c'};
        String string=new String(arr1);//数组转字符串
        System.out.println(string);

        String s=String.format("%d-%d-%d",2004,9,17);//格式化
        System.out.println(s);
    }



static class Student{
        public String name;
        public int age;
        public Student(String name, int age){
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    public static void main2(String[] args) {
        int a=123;
        String str=String.valueOf(a);//数字转字符串
        System.out.println(str);
        //也可以把一个对象变为字符串
        Student student= new Student("John", 12);
        String str1=String.valueOf(student);
        System.out.println(str1);

        System.out.println("===============");
        //字符串转数字
        int i=Integer.parseInt("123");//字符串转int类型数字
        double d=Double.parseDouble("123");//转double
        System.out.println(i);
        System.out.println(d);
    }




    public static void main1(String[] args) {
        String str="abcdecd";
        char ch=str.charAt(1);//返回下标1对应的字母
        System.out.println(ch);

        //遍历
//        for (int i = 0; i < str.length(); i++) {
//            char ch1=str.charAt(i);
//            System.out.print(ch1+" ");
//        }

        int index=str.indexOf("c",2);//返回该字符第一次出现的位置,从数字2下标开始找
        int index1=str.lastIndexOf("c",5);//倒着看过来字符c第一次出现的位置，从数字5下标开始
        int index2=str.lastIndexOf("cde");//找字符串
        int index3=str.lastIndexOf("cd");//倒过来找

        //返回值都是下标
        System.out.println(index);
        System.out.println(index1);
        System.out.println(index2);
        System.out.println(index3);
    }
}
